//

import UIKit

class WP_WeeklyViewController: WP_BasicViewController {
    
    var isPush:Bool = false
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        
         WP_CoreDataModel.coreDataManager.setEntityName(text: "WeekModel")
        getData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavTitle("Weekly")
        // Do any additional setup after loading the view.
        weak var weakSelf = self
        setupRightSetting(UIImage(named: "arrow39") as Any) {
            
            let vc = WP_WeeklyAddViewController()
            weakSelf?.navigationController?.pushViewController(vc, animated: true)
            
        }
        self.view.addSubview(weekTableView)
        
    }
    
    
    lazy var weekTableView:UITableView = {
        
        let tableview = UITableView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - kNavBarHeight), style: .plain)
        tableview.delegate = self
        tableview.dataSource = self
        tableview.rowHeight = kRatioHeight(210)
        tableview.tableFooterView = UIView()
        tableview .register(WP_WeekListTableViewCell.classForCoder(), forCellReuseIdentifier:NSStringFromClass(WP_WeekListTableViewCell.classForCoder()))
        tableview.separatorColor = UIColor.white
        return tableview
        
    }()
    
    lazy var dataArray:[Any] = {
        let array = Array<Any>()
        return array
    }()
    
    
    func getData()  {
        
         WP_CoreDataModel.coreDataManager.readData(sequenceKeys:Array(), ascending: false, filterStr: nil, success: { (arr) in
            
            if arr.isEmpty && isPush == false {
                let vc = WP_WeeklyAddViewController()
                self.navigationController?.pushViewController(vc, animated: true)
                isPush = true
                return
            }
            self.dataArray = arr
            weekTableView.reloadData()
           
        }, fail: {})
        
    }
    
}

extension WP_WeeklyViewController:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:WP_WeekListTableViewCell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(WP_WeekListTableViewCell.classForCoder()), for: indexPath) as! WP_WeekListTableViewCell
        cell.selectionStyle = .none
        
        let model = dataArray[indexPath.section] as! WeekModel
        
        //        cell.delegate = self;
        cell.setModel(date: model.date, finish: model.finish, unfinish: model.unfinish, summary: model.summary, plan: model.plan, remark: model.remark)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColorFromHex(0xF0FFFF)
        return view
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    //*
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool{
        return true
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?{
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
    {
        let model = dataArray[indexPath.section] as! WeekModel
        
        if editingStyle == .delete {
            print(model.iD!)
             WP_CoreDataModel.coreDataManager.deleteData(key: "iD = \(model.iD!)", success: {getData()}, fail: {})
        }
        
    }
    //*/
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let model = dataArray[indexPath.section] as! WeekModel
        let vc = WP_WeekDetailViewController()
        vc.model = model
        vc.ID = model.iD
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}
